/**
 * @(#)JDOMailSubscribeStorage.java, 2010-5-16. 
 * 
 * Copyright 2010 Youdao, Inc. All rights reserved.
 * YOUDAO PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.rsstuan.storage.impl;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;

import com.rsstuan.storage.data.MailSubscribeData;
import com.rsstuan.storage.data.TuanGouChannelData;

/**
 *
 * @author guosq
 *
 */
public class JDOMailSubscribeStorage {

    
    private static final Logger LOG = Logger.getLogger(JDOMailSubscribeStorage.class.getName());
    
    public boolean save(MailSubscribeData sub) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            pm.makePersistent(sub);
            return true;
        } 
        catch (Exception e) {
        	LOG.log(Level.WARNING, "save email failed" , e);
            return false;
        }
        finally {
            pm.close();
        }
    }
    
    public List<MailSubscribeData> getAllSubs() {
        PersistenceManager pm = PMF.get().getPersistenceManager(); 
        String query = "select from " + MailSubscribeData.class.getName();
        List<MailSubscribeData> subs = (List<MailSubscribeData>) pm.newQuery(query).execute();
        return subs;
    }
    
    public boolean exist(String email, String city) {
    	PersistenceManager pm = PMF.get().getPersistenceManager(); 
        String query = "select from " + MailSubscribeData.class.getName() + " where email=='" + email + "' && city=='" + city + "'";
        List<MailSubscribeData> subs = (List<MailSubscribeData>) pm.newQuery(query).execute();
        if (subs != null && subs.size() > 0)
        	return true;
        return false;
    }
}
